package com.atguigu.edu.realtime.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

    private static JedisPool jedisPool;

    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(2000);

        jedisPool = new JedisPool(jedisPoolConfig,"hadoop102",6379,10000);
    }

    //获取Jedis
    public static Jedis getJedis(){
        System.out.println("~~~创建Jedis~~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
    //关闭Jedis
    public static void closeJedis(Jedis jedis){
        System.out.println("~~~关闭Jedis~~~");
        if(jedis != null){
            jedis.close();
        }
    }

    //获取支持异步操作Redis的客户端连接对象
    public static StatefulRedisConnection<String, String> getRedisAsyncConnection() {
        System.out.println("~~~获取异步操作Redis的客户端~~~");
        StatefulRedisConnection<String, String> asyncRedisConn = RedisClient.create("redis://hadoop102:6379/0").connect();
        return asyncRedisConn;
    }
    //关闭支持异步操作Redis的客户端连接对象
    public static void closeRedisAsyncConnection(StatefulRedisConnection<String, String> asyncRedisConn) {
        System.out.println("~~~关闭异步操作Redis的客户端~~~");
        if(asyncRedisConn != null && asyncRedisConn.isOpen()){
            asyncRedisConn.close();
        }
    }



    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String pong = jedis.ping();
        System.out.println(pong);
        closeJedis(jedis);
    }

    //从Redis中读取维度数据
    public static JSONObject readDim(Jedis jedis,String tableName,String id){
        //拼接key
        String key = getKey(tableName,id);
        //根据key到Redis中获取对应的维度数据
        String dimJsonStr = jedis.get(key);
        //判断是否缓存命中
        if(StringUtils.isNotEmpty(dimJsonStr)){
            //缓存命中
            JSONObject dimJsonObj = JSON.parseObject(dimJsonStr);
            return dimJsonObj;
        }
        return null;
    }

    public static String getKey(String tableName, String id) {
        return tableName + ":" + id ;
    }
    //向Redis中放维度数据
    public static void writeDim(Jedis jedis,String tableName,String id,JSONObject dimJsonObj){
        //拼接key
        String key = getKey(tableName, id);
        //将维度数据缓存到Redis中，并设置缓存的失效时间为1day
        jedis.setex(key,24 * 3600,dimJsonObj.toJSONString());
    }

    //以异步的方式从Redis中读取维度数据
    public static JSONObject readDimAsync( StatefulRedisConnection<String, String> asyncRedisConn,String tableName,String id){
        //拼接key
        String key = getKey(tableName, id);
        try {
            String dimJsonStr = asyncRedisConn.async().get(key).get();
            if(StringUtils.isNotEmpty(dimJsonStr)){
                JSONObject dimJsonObj = JSON.parseObject(dimJsonStr);
                return dimJsonObj;
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    //以异步的方式向Redis中放维度数据
    public static void writeDimAsync(StatefulRedisConnection<String, String> asyncRedisConn,String tableName,String id,JSONObject dimJsonObj){
        //拼接key
        String key = getKey(tableName, id);
        asyncRedisConn.async().setex(key,24*3600, dimJsonObj.toJSONString());
    }
}